import {
	getHospList,
	getDoctorList,
	enterDeptMiddlePage
} from '@/api/order.js'


const state = {
	hotDoc: [], //紧俏医生，医生中间页数据
	hotDep: [], //科室中间页数据
	teamId: '',
	groupEdit: [], //标签回显
	showSelPatient: false, //患者列表中，患者选择是否能选
	teamTitle: '', //返回时团队的title显示
	filterChosee: [], //患者不限标签那里的回显
	FilterTeamChoosee: [], //患者全部添加人那里的回显
	tohuanDetailsParams: {
		cuId: '',
		id: ''
	},
	JobTempList: [
		// {
		// 	id: 9,
		// 	title: '慢性湿疹治疗回访',
		// 	discription: '湿疹是由于复杂的内外因素激发而引起的一而引起的'
		// },
	],
	JobList: [], //任务规则列表
	JobRuleForm: { //添加任务规则页面
		jobType: '1', //任务类型  1定时 2周期
		jobName: '', //任务名称
		jobContent: '', //任务内容
		jobStartTime: '', //任务时间
		jobCycle: 0 ,//几天，周期
		quesTemplateId:'' //所选模板ID
	},
	newproject:{  //创建随访计划
		formName:'',
		userList:[],
		huanzheList:[],
		MarkList:[],
		editTagIdList:[],
		Timehuixian:'',
		planId:'',
		isShow:false,
		edit:'',
		currentCondition:'',
		showPages:1,
		inactive: false,
		projectTime: {
			startDate: '',
			endDate: ''
		},
	},
	JobType: '定时任务', //添加任务规则页面-》任务类型所选值
	suifangTemplate: '', //所选模板信息，诊后随访医生端
	suifangUserListSelected: '', //随访所选患者
	suifanghuanzheListSelected: '' ,//随访所选患者标签
	indexPinggu:''   //待评估把数据从首页带到输入框页面
};

const mutations = {
	EditGroup(state, res) {
		state.groupEdit = res
	},
	GETDOCLIST(state, res) {
		state.hotDoc = res
	},
	GETKESHILIST(state, res) {
		//console.log(res,'00000')
		state.hotDep = res
	},
	TEAMIDS(state, res) {
		state.teamId = res
	},
	PATIENTSHOW(state, res) {
		state.showSelPatient = res
	},
	TEAMTITLESET(state, res) {
		state.teamTitle = res
	},
	FilterChoose(state, res) {
		state.filterChosee = []
		state.filterChosee = res
	},
	FilterTeamChoose(state, res) {
		state.FilterTeamChoosee = res
	},
	DETAILSPARAMS(state, res) {
		state.tohuanDetailsParams.cuId = res.cuId
		state.tohuanDetailsParams.id = res.id
	},
	PICKTemplate(state, res) {
		state.suifangTemplate = res
	},
	SUIFANGuserSelected(state, res) {
		state.suifangUserListSelected = res
	},
	SUIFANGhuanzheSelected(state, res) {
		state.suifanghuanzheListSelected = res
	},
	JobFORM(state, res) {
		state.JobRuleForm = res
	},
	JobFORMDefaultTime(state, res) {
		const now = new Date();
		// 获取年份  
		const year = now.getFullYear();
		// 获取月份，注意月份是从0开始的，所以需要+1  
		const month = String(now.getMonth() + 1).padStart(2, '0');
		// 获取日期  
		const day = String(now.getDate()).padStart(2, '0');
		// 获取小时  
		const hour = String(now.getHours()+1).padStart(2, '0');
		// 获取分钟  
		const minute = String(now.getMinutes()).padStart(2, '0');
		// 拼接年月日时分  
		const formattedDateTime = `${year}-${month}-${day} ${hour}:${minute}`;
		state.JobRuleForm.jobStartTime = formattedDateTime
	},
	JobFORMClear(state, res) {
		state.JobRuleForm = { //添加任务规则页面
			jobType: '1', //任务类型  1定时 2周期
			jobName: '', //任务名称
			jobContent: '', //任务内容
			jobStartTime: '', //任务时间
			jobCycle: 0 ,//几天，周期
			quesTemplateId:'' //所选模板ID
		}
	},
	JOBTYPEShow(state, res) {
		state.JobType = res
	},
	JOBTEMList(state, res) {
		state.JobTempList = res
	},
	JobPushList(state, res) {
		//console.log(444);
		state.JobList.push(res);
	},
	DELJOB(state, res) {
		let index = state.JobList.findIndex(v => v.jobName == res.jobName)
		if (res.jobName) {
			state.JobList.splice(index, 1)
		}
		//console.log(res);
	},
	SHOWJOBLISTALL(state, res) {
		state.JobList = res
	},
	PINGGUDETAILS(state, res){
		state.indexPinggu = res
	},
	TEMPlateId(state, res){
		state.JobRuleForm.quesTemplateId = res
	}
};

const actions = {
	addJobPushList({
		state,
		commit
	}, res) {
		//console.log(state.JobList,res);
		if (state.JobList.some(item => item.jobName == res.jobName)) {
			return Promise.resolve(false);
		} else {
			commit('JobPushList', res);
			return Promise.resolve(true);
		}
	},
	editJobList({
		state,
		commit
	}, jobName) {
		let findIndex = state.JobList.findIndex(item => item.jobName == jobName)
		//console.log(findIndex, 'sss');
		if (findIndex != -1) {
			//state.JobRuleForm = JSON.parse(JSON.stringify(filter[0])) //深拷贝
			state.JobRuleForm.jobType = state.JobList[findIndex].jobType
			state.JobRuleForm.jobName = state.JobList[findIndex].jobName
			state.JobRuleForm.jobContent = state.JobList[findIndex].jobContent
			state.JobRuleForm.jobStartTime = state.JobList[findIndex].jobStartTime
			state.JobRuleForm.jobCycle = state.JobList[findIndex].jobCycle
			state.JobRuleForm.quesTemplateId = state.JobList[findIndex].quesTemplateId
			//state.JobRuleForm = state.JobList[findIndex]
			return {
				isEdit: true,
				find: true
			}
		} else {
			return {
				isEdit: true,
				find: false
			}
		}
	},
	confirmEditJob({
		state,
		commit
	}, jobName) {
		let findIndex = state.JobList.findIndex(item => item.jobName == jobName)
		if (findIndex != -1) {
			//state.JobRuleForm = JSON.parse(JSON.stringify(filter[0])) //深拷贝
			state.JobList[findIndex].jobType = state.JobRuleForm.jobType
			state.JobList[findIndex].jobName = state.JobRuleForm.jobName
			state.JobList[findIndex].jobContent = state.JobRuleForm.jobContent
			state.JobList[findIndex].jobStartTime = state.JobRuleForm.jobStartTime
			state.JobList[findIndex].jobCycle = state.JobRuleForm.jobCycle
			//state.JobRuleForm = state.JobList[findIndex]
		}
	},
	delJobList({
		state,
		commit
	}, res) {
		// let filter = state.JobList.filter(item=>item.jobName == res.jobName)
		// if(filter.length > 0){
		// 	state.JobList = []
		// 	state.JobList.push(filter)	
		// }
	},
	async doclist({
		commit
	}, data) {
		let res = await getDoctorList(data)
		commit('GETDOCLIST', res)
		//return '111'
	},
	async keshilist({
		commit
	}, data) {
		let res = await enterDeptMiddlePage(data)
		//console.log(res,'!!!')
		commit('GETKESHILIST', res)
	},
	async phoneNumber({
		commit
	}, number) {
		if (number && number.length > 0) {
			return number.slice(0, 3) + '****' + number.slice(7, 11)
		}
		return number
	},
};

export default {
	state,
	mutations,
	actions
};